﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Eis51.CampanyCMS.Model;
using Eis51.CampanyCMS.Model.BaseInfoMng.QueryO;
using Eis51.CampanyCMS.Business.BaseInfoMng;
using Eis51.CampanyCMS.Model.BaseInfoMng;
using System.IO;
using Eis51.Web.Admin;

namespace Eis51.CampanyCMS.WebUI.Admin.SystemManager
{
    public partial class TemplateSelect : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtOldTemplateID.Value = AppContext.CurrentAdministrator.TemplateId.ToString();
                txtDefaultHomePage.Text = AppContext.CurrentAdministrator.Defaulthomepage;
                txtHtmlCreateFolder.Text = AppContext.CurrentAdministrator.HtmlCreateFolder;
                BaseTemplatesQuery query1 = new BaseTemplatesQuery();
                query1.TempName = string.Empty;
                rpTemplate.DataSource = BaseTemplatesBLL.GetInstance().SearchByCondition(query1);
                this.rpTemplate.DataBind();

            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Administrator obj = AppContext.CurrentAdministrator;
            
            BaseTemplates temp = GetSelectTemplateID();
            if (temp != null)
            {
                obj.TemplateId = temp.TemplateId;
                obj.Mytemplatepath = temp.TemplateFloder;
                obj.Defaulthomepage = txtDefaultHomePage.Text;
                obj.HtmlCreateFolder = txtHtmlCreateFolder.Text;

                string depath = Server.MapPath(obj.HtmlCreateFolder);
                if (!new System.IO.DirectoryInfo(depath).Exists)
                {
                    Directory.CreateDirectory(depath);
                    //copy ，NewsSearch.aspx,PSearch.aspx
                    string c1 = Server.MapPath("~/NewsSearch.aspx");
                    string c2 = Server.MapPath("~/PSearch.aspx");
                    string nc1 = Path.Combine(depath, "NewsSearch.aspx");
                    string nc2 = Path.Combine(depath, "PSearch.aspx");
                    FileInfo fi = new FileInfo(c1);
                    fi.CopyTo(nc1, true);
                    fi = new FileInfo(c2);
                    fi.CopyTo(nc2);
                }
            }
            AdministratorBLL.GetInstance().Update(obj);
            AppContext.CurrentAdministrator = obj;
        }



        protected void rpTemplate_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem
                || e.Item.ItemType == ListItemType.Item)
            {
                ((RadioButton)e.Item.FindControl("selTemplate")).Attributes.Add("onclick", "setRadio(this)");
                HiddenField txtTemplateID = ((HiddenField)e.Item.FindControl("txtTemplateID"));
                if (txtTemplateID.Value == txtOldTemplateID.Value)
                {
                    ((RadioButton)e.Item.FindControl("selTemplate")).Checked = true;
                }

                Image Image1 = ((Image)e.Item.FindControl("Image1"));
                BaseTemplates tmp1 = BaseTemplatesBLL.GetInstance().Get(new BaseTemplates.Key(int.Parse(txtTemplateID.Value)));
                Image1.ImageUrl = tmp1.TemplateThumbnailPath;

            }
        }

        private BaseTemplates GetSelectTemplateID()
        {
            foreach (RepeaterItem tmp in this.rpTemplate.Items)
            {
                RadioButton selectExpresss = ((RadioButton)tmp.FindControl("selTemplate"));
                if (selectExpresss != null && selectExpresss.Checked)
                {
                    HiddenField txtTemplateID = ((HiddenField)tmp.FindControl("txtTemplateID"));
                    return BaseTemplatesBLL.GetInstance().Get(new BaseTemplates.Key(int.Parse(txtTemplateID.Value)));
                }
            }
            return null;
        }
    }
}
